$MAX_NODE_ID = 1300;
$SINK_ID = 60001;
#$SINK_ID = 0;

my @AllLostPacket_NodeId=();
my @AllLostPacket_Type = ();
my @AllLostPacket_Seq=();
my @AllTime1 = ();
my @AllTime2 = ();
my $all_lost_packet="./output/AllLostPackets.dat";

print "reading lost file $all_lost_packet...\n";
open LOST_PACKET, "<$all_lost_packet" or die "cannot open $all_lost_packet";

open UNKNOWN_PACKET, ">./output/unknownpackets.dat" or die "cannot open unknown packets file";

$lostPacketNo = 0;
while(<LOST_PACKET>)
{
	chomp;
	@packet = split(' ');
	$AllLostPacket_NodeId[$lostPacketNo] = $packet[0];
	$AllLostPacket_Type[$lostPacketNo] = hex("$packet[1]");
	$AllLostPacket_Seq[$lostPacketNo] = $packet[2];
	$AllTime1[$lostPacketNo] = $packet[3];
	$AllTime2[$lostPacketNo] = $packet[4];
	#print "$AllLostPacket_NodeId[$lostPacketNo] $AllLostPacket_Seq[$lostPacketNo] $AllLostPacket_Type[$lostPacketNo]\n";
	$lostPacketNo++;
}


print "lostPacketNo=$lostPacketNo, sorting...\n";
@rank = sort {$AllLostPacket_NodeId[$a] <=> $AllLostPacket_NodeId[$b] or
	$AllLostPacket_Seq[$a] <=> $AllLostPacket_Seq[$b] or
	$AllLostPacket_Type[$a] <=> $AllLostPacket_Type[$b]
	} 0..$#AllLostPacket_NodeId;  #sort the data according to the source id, seq and type;

@AllLostPacket_NodeId = @AllLostPacket_NodeId[@rank];
@AllLostPacket_Seq = @AllLostPacket_Seq[@rank];
@AllLostPacket_Type = @AllLostPacket_Type[@rank];
@AllTime1 = @AllTime1[@rank];
@AllTime2 = @AllTime2[@rank];

print "saving sorted results in alllost.dat...\n";
open ALL_LOST2, ">./result/alllost.dat" or die "cannot open alllost.dat";
for($tempi = 0; $tempi < @AllLostPacket_NodeId; ++$tempi)
{
	print ALL_LOST2 $AllLostPacket_NodeId[$tempi], " ", $AllLostPacket_Seq[$tempi], " ", $AllLostPacket_Type[$tempi], "\n";
}
close ALL_LOST2;

$finallostinformation = ".\\result\\lostcauses.dat";
open FINAL_LOST_INFORMATION, ">$finallostinformation" or die "cannot open $finallostinformation\n";
open MSGPOOL_FILE, ">.\\result\\msgpool.dat" or die "cannot open msgpool.dat\n";

@AllLostCauses = ();
@AllLostPositions = ();
@AllLostTime1 = ();
@AllLostTime2 = ();
@AllLostSource = ();
@AllLostSeq = ();
@AllLostType = ();

$i = 0;
while($i < $lostPacketNo)
{
	$nodeId = $AllLostPacket_NodeId[$i];		

	$logFileName = ".\\C5log\\". $nodeId . ".log";
	print "processing log file $logFileName...\n";
	#print $logFileName, " ";
	$noFile = 0;
	open INPUT, "<$logFileName " or $noFile = 1;
	if ($noFile)#no file for this id, pass packet losses of this id;
	{
		print "no file $logFileName\n";
		while($AllLostPacket_NodeId[$i] == $nodeId && $i < $lostPacketNo)
		{
			++$i;
		}
	}else #file found, find corresponding seq in the file.
	{
		print "find file $logFileName\n";
		#read the log file.
		$logNum = 0;
		@originId = ();
		@originSeq = ();
		@packetType = ();
		@controlType = ();
		@controlType_origin = ();
		@nextHop = ();
		@timestamp = ();
		@rssi = ();
		@logSeq = ();
		@logNode = ();
		print "reading log file...\n";
		while (<INPUT>)
		{
			@rs = split(' ');
			$arraySize = @rs;
			$originId[$logNum] = hex("$rs[10]$rs[11]"); 
			$originSeq[$logNum] = hex("$rs[12]$rs[13]");
			$packetType[$logNum] = hex("$rs[14]"); 
			$controlType[$logNum] = hex("$rs[9]"); 
			$controlType_origin[$logNum] = $rs[9];
			$nextHop[$logNum] = hex("$rs[15]$rs[16]"); 
			$timestamp[$logNum] = hex("$rs[17]$rs[18]$rs[19]$rs[20]"); 
			$rssi[$logNum] = hex("$rs[21]");  
			$logSeq[$logNum] = hex("$rs[22]$rs[23]"); 
			$logNode[$logNum] = $rs[0];
			$logNum++;
		}
		if ($logNum > 0){
			@rank = sort {$originId[$a] <=> $originId[$b] or
				$originSeq[$a] <=> $originSeq[$b] or
				$packetType[$a] <=> $packetType[$b]
				} 0..$#originId;  #sort the data according to the source id, seq and type;
			@originId = @originId[@rank];
			@originSeq = @originSeq[@rank];
			@packetType = @packetType[@rank];
			@controlType = @controlType[@rank];
			@controlType_origin = @controlType_origin[@rank];
			@nextHop = @nextHop[@rank];
			@timestamp = @timestamp[@rank];
			@rssi = @rssi[@rank];
			@logSeq = @logSeq[@rank];
			@logNode = @logNode[@rank];
			#end read
			
			print "writing new log2 file...\n";
			$newlogfile = ".\\result\\". "$nodeId.log2";
			open OUTPUTLOG, ">$newlogfile" or die "no file $newlogfile\n";
			for($tempi = 0; $tempi < @originId; ++$tempi){
				print OUTPUTLOG "$originId[$tempi] $originSeq[$tempi] $packetType[$tempi] $controlType_origin[$tempi] $logNode[$tempi]\n";		
			}
			close OUTPUTLOG;
		}
		$logIndex = 0;
#		while ($logIndex < $logNum && $originId[$logIndex] < $nodeId)
#		{
#			++$logIndex;
#		}

		print "processing node $nodeId...\n";
		$lostinformation = ".\\result\\"."$nodeId.lost";
		$lostinformation2 = ".\\result\\"."$nodeId.lost2";
		#$msgpoolfile = ".\\result\\"."$nodeId-msgpool.dat";
		open LOST_INFORMATION, ">$lostinformation" or die "cannot open $lostinformation\n";
		open LOST_INFORMATION2, ">$lostinformation2" or die "cannot open $lostinformation2\n";
		while($AllLostPacket_NodeId[$i] == $nodeId && $i < $lostPacketNo)
		{
			#check the seq, if lost packet seq > log or seq == log, type > log, move log, else, move lost packet.
			#check both the seq and type.
			#print $AllLostPacket_NodeId[$i], " ", $AllLostPacket_Seq[$i], " ", $AllLostPacket_Type[$i], "\n";

			while ($logIndex < $logNum 
					&& ($originId[$logIndex] < $nodeId 
							|| ($originId[$logIndex] == $nodeId && $AllLostPacket_Seq[$i] > $originSeq[$logIndex])
							|| ($AllLostPacket_Seq[$i] == $originSeq[$logIndex] && $AllLostPacket_Type[$i] > $packetType[$logIndex])))
			{
				#print "not match $originId[$logIndex] $originSeq[$logIndex] $packetType[$logIndex] $logIndex\n";
				++$logIndex;# move log, smilar to merge sort.
			}
			if ($logIndex < $logNum && $AllLostPacket_NodeId[$i] == $originId[$logIndex] && $AllLostPacket_Seq[$i] == $originSeq[$logIndex] && $AllLostPacket_Type[$i] == $packetType[$logIndex])
			{
				
				#print "----------matched $nodeId $originId[$logIndex] $originSeq[$logIndex] $packetType[$logIndex]\n";
				print LOST_INFORMATION "$AllLostPacket_NodeId[$i] seq:$AllLostPacket_Seq[$i] type:$AllLostPacket_Type[$i] ";
				@node1 = (), @node2 = (), @eventtype = ();
				while ($AllLostPacket_NodeId[$i] == $originId[$logIndex] && $AllLostPacket_Seq[$i] == $originSeq[$logIndex] && $AllLostPacket_Type[$i] == $packetType[$logIndex])
				{
					$tempControlType = $controlType[$logIndex];
					if(0xAA==$tempControlType)#receive from son
					{
						print LOST_INFORMATION "$nextHop[$logIndex]->$logNode[$logIndex] recvd ";	
						push(@node1, $nextHop[$logIndex]);
						push(@node2, $logNode[$logIndex]);
						push(@eventtype, "recvd");
					}elsif(0xFA==$tempControlType)#retransmit**********************************************************************************
					{
					  print LOST_INFORMATION "$logNode[$logIndex]->$nextHop[$logIndex] retrans ";	
						push(@node1, $logNode[$logIndex]);		
						push(@node2, $nextHop[$logIndex]);
						push(@eventtype, "retrans");
					}elsif(0xFF==$tempControlType)#acked trasnsmit
					{
						print LOST_INFORMATION "$logNode[$logIndex]->$nextHop[$logIndex] acked ";	
						push(@node1, $logNode[$logIndex]);		
						push(@node2, $nextHop[$logIndex]);
						push(@eventtype, "acked");		
					}
					elsif(0xFD==$tempControlType)#DUPLICATE_CACHE_ATClient_SEND   check whether the dropped packet is generated by itself  if($rs[14] == $originId)
					{
						
					}
					elsif(0xAD==$tempControlType)#DUPLICATE_CACHE--------check whether the dropped packet is generated by itself  if($rs[14] == $originId)
					{
						print LOST_INFORMATION "$logNode[$logIndex]->$nextHop[$logIndex] duplicatedCach ";	
						push(@node1, $nextHop[$logIndex]);
						push(@node2, $logNode[$logIndex]);		
						push(@eventtype, "duplicatedCache");		
					}
					elsif(0xAE==$tempControlType)#DUPLICATE_PACKET--------------check whether the dropped packet is generated by itself  if($rs[14] == $originId)
					{
						print LOST_INFORMATION "$logNode[$logIndex]->$nextHop[$logIndex] duplicated ";	
						push(@node1, $nextHop[$logIndex]);	
						push(@node2, $logNode[$logIndex]);	
						push(@eventtype, "duplicated");		
					}
					elsif(0xFE==$tempControlType)#NO_ROUTE
					{
						
					}
					elsif(0xFB==$tempControlType)  #retransmit drop
					{
						print LOST_INFORMATION "$logNode[$logIndex]->$nextHop[$logIndex] retrans drop ";			
						push(@node1, $logNode[$logIndex]);		
						push(@node2, $nextHop[$logIndex]);
						push(@eventtype, "retransdrop");	
					}
					elsif(0xBB==$tempControlType)#MSG_POOL_EMPTY
					{
						print LOST_INFORMATION "$logNode[$logIndex] msg pool drop ";			
						push(@node1, $logNode[$logIndex]);		
						push(@node2, $nextHop[$logIndex]);
						push(@eventtype, "msgpooldrop");	
						print "+++++++++++++$AllLostPacket_NodeId[$i] seq:$AllLostPacket_Seq[$i] type:$AllLostPacket_Type[$i] $logNode[$logIndex] msg pool drop \n";
						print MSGPOOL_FILE "$AllLostPacket_NodeId[$i] seq:$AllLostPacket_Seq[$i] type:$AllLostPacket_Type[$i] $logNode[$logIndex] msg pool drop \n";
						#system("pause");
					}
					elsif(0xCC==$tempControlType)#queue entry pool
					{
						print LOST_INFORMATION "$logNode[$logIndex] queue pool drop ";			
						push(@node1, $logNode[$logIndex]);		
						push(@node2, $nextHop[$logIndex]);
						push(@eventtype, "queuepooldrop");	
						print "++++++++++++$AllLostPacket_NodeId[$i] seq:$AllLostPacket_Seq[$i] type:$AllLostPacket_Type[$i] $logNode[$logIndex] queue pool drop \n";
						print MSGPOOL_FILE "$AllLostPacket_NodeId[$i] seq:$AllLostPacket_Seq[$i] type:$AllLostPacket_Type[$i] $logNode[$logIndex] queue pool drop \n";
					}
					elsif(0xDD==$tempControlType)#SEND_QUEUE_FULL--for clients' packets
					{
						print LOST_INFORMATION "$logNode[$logIndex] queue client drop ";			
						push(@node1, $logNode[$logIndex]);		
						push(@node2, $nextHop[$logIndex]);
						push(@eventtype, "queueclientdrop");	
					}
					else {
						print LOST_INFORMATION "event: $controlType_origin[$logIndex]\n ";
					}
					++$logIndex;
				}
				print LOST_INFORMATION "\n";
				
				@newnode1 = (), @newnode2 = (), @neweventtype = ();
				#print "analyzing...\n";
				&AnalyzeCause($AllLostPacket_NodeId[$i], $SINK_ID);
				print LOST_INFORMATION "$AllLostPacket_NodeId[$i] seq:$AllLostPacket_Seq[$i] type:$AllLostPacket_Type[$i] ";
				for ($tempj = 0; $tempj < @newnode1; ++$tempj)
				{
					print LOST_INFORMATION "$newnode1[$tempj]->$newnode2[$tempj] $neweventtype[$tempj] ";
				}
				print LOST_INFORMATION "\n";
 
 				our $lostCause = "unknown";
 				$lostPosition = "unknown";
 				$lastSenderAfterAnalysis = -1;
 				$lastReceiverAfterAnalysis = -1;
 				$lastEventAfterAnalysis = "unknown";
 				&AnalyzeLossCause();
 				$tempTime1 = @AllTime1[$i];
 				$tempTime2 = @AllTime2[$i];
 				print FINAL_LOST_INFORMATION "$AllLostPacket_NodeId[$i] seq:$AllLostPacket_Seq[$i] type:$AllLostPacket_Type[$i]\t\t$lastSenderAfterAnalysis\t\t$lastReceiverAfterAnalysis\t$lastEventAfterAnalysis\t$lostPosition\t$lostCause\t\t$tempTime1\t$tempTime2\n";
 				

 				push(@AllLostTime1, $tempTime1);
 				push(@AllLostTime2, $tempTime2);
 				push(@AllLostCauses, $lostCause);
 				push(@AllLostPositions, $lostPosition);
 				push(@AllLostSource, $AllLostPacket_NodeId[$i]);
 				push(@AllLostSeq, $AllLostPacket_Seq[$i]);
 				push(@AllLostType, $AllLostPacket_Type[$i]);
			}else
			{
				print LOST_INFORMATION "$AllLostPacket_NodeId[$i] $AllLostPacket_Seq[$i], $AllLostPacket_Type[$i] log not found\n";
				print LOST_INFORMATION "$AllLostPacket_NodeId[$i] $AllLostPacket_Seq[$i], $AllLostPacket_Type[$i] log not found\n";
				print UNKNOWN_PACKET "$AllLostPacket_NodeId[$i] $AllLostPacket_Seq[$i], $AllLostPacket_Type[$i] log not found\n";
				$tempTime1 = @AllTime1[$i];
 				$tempTime2 = @AllTime2[$i];
 				print FINAL_LOST_INFORMATION "$AllLostPacket_NodeId[$i] seq:$AllLostPacket_Seq[$i] type:$AllLostPacket_Type[$i]\t\t-2\t\t-2\tunknown\tunknown\tunknown\t\t$tempTime1\t$tempTime2\n";


 				
 				push(@AllLostTime1, $tempTime1);
 				push(@AllLostTime2, $tempTime2);
 				push(@AllLostCauses, "unknown");
 				push(@AllLostPositions, "unknown");
 				push(@AllLostSource, $AllLostPacket_NodeId[$i]);
 				push(@AllLostSeq, $AllLostPacket_Seq[$i]);
 				push(@AllLostType, $AllLostPacket_Type[$i]);
			}
			++$i;# move lost packet
		}
		close LOST_INFORMATION;
	}
}
close FINAL_LOST_INFORMATION;
close MSGPOOL_FILE;

close UNKNOWN_PACKET;
%lostSourceCount = ();
%lostCauseCount = ();
%lostPositionCount = ();
for ($tempi = 0; $tempi < @AllLostCauses; ++$tempi){
	$lostSourceCount{$AllLostSource[$tempi]} += 1;
	$lostCauseCount{$AllLostCauses[$tempi]} += 1;
	$lostPositionCount{$AllLostPositions[$tempi]} += 1;
}

open RESULT_FILE, ">./output/LostCauses.dat" or die "cannot open LostCauses.dat\n";

foreach $key (keys %lostSourceCount){
	print "lostsource\t$key $lostSourceCount{$key}\n";
	print RESULT_FILE "lostsource\t$key $lostSourceCount{$key}\n";

}

foreach $key (keys %lostCauseCount){
	print "lostCause\t$key $lostCauseCount{$key}\n";
	print RESULT_FILE "lostCause\t$key $lostCauseCount{$key}\n";

}

foreach $key (keys %lostPositionCount){
	print "lostPosition\t$key $lostPositionCount{$key}\n";
	print RESULT_FILE "lostPosition\t$key $lostPositionCount{$key}\n";
}
close RESULT_FILE;
system("pause");


sub AnalyzeCause{
	my $tempi = 0;
	my ($lastsender, $tempsink) = (shift, shift);
	my $lastreceiver = $lastsender;
	my @considered = ();
	my $needStop = 0, $currentState = "start";
	#my @nodesWOIncommingLink = ();
	my $currentState = "start";
	my $tempNextState = "start";
	while (!$needStop){
		$lastchoice = -1;
		$lastevent = -1;
		$tempNextState = "start";

		for ($tempi = 0; $tempi < @node1; ++$tempi){
			if ($considered[$tempi]){
				next;
			}
			my $thisevent = $eventtype[$tempi];
			my $thisnode1 = $node1[$tempi];
			my $thisnode2 = $node2[$tempi];
		#	print "($currentState $lastsender $lastreceiver)$thisnode1->$thisnode2 $thisevent $tempNextState\n ";

			if ($thisnode1 == $lastsender){
				if ($currentState eq "start"){
					#print "thisevent:$thisevent tempNextState:$tempNextState \n";
					if (($thisevent eq "trans" || $thisevent eq "retrans")){
						$lastchoice = $tempi;
						$tempNextState = "n1trans";
						last;
					}
					if ($thisevent eq "acked" && $tempNextState ne "n1trans") {
						$lastchoice = $tempi;
						$tempNextState = "n2acked";
					}
					if ($thisevent eq "recvd" && $tempNextState ne "n1trans" && $tempNextState ne "n2acked"){
						$lastchoice = $tempi;
						$tempNextState = "n2recvd";
					}
					if (($thisevent eq "msgpooldrop" || $thisevent eq "queuepooldrop" 
						 || $thisevent eq "queueclientdrop"|| $thisevent eq "duplicatedCache" || $thisevent eq "duplicated")
							&&$tempNextState ne "drop" && $tempNextState ne "n1trans" 
							&& $tempNextState ne "n2acked" && $tempNextState ne "n2recvd"){
						$lastchoice = $tempi;
						$tempNextState = "drop";
					}
				}
				if ($currentState eq "n1trans"){
					if ($thisevent eq "retrans"){
						$lastchoice = $tempi;
						$tempNextState = "n1trans";
						last;
					}elsif ($thisevent eq "acked" && $tempNextState ne "n1trans"){
						$lastchoice = $tempi;
						$tempNextState = "n2acked";
						last;
					}elsif ($thisevent eq "recvd" && $tempNextState ne "n1trans" && $tempNextState ne "n2acked"){
						$lastchoice = $tempi;
						$tempNextState = "n2recvd";
					}
				}
				
				if ($currentState eq "n2acked"){
					if ($thisevent eq "recvd"){
						$lastchoice = $tempi;
						$tempNextState = "n2recvd";
						last;
					}
				}
				
				if (($thisevent eq "msgpooldrop" || $thisevent eq "queuepooldrop" 
					 || $thisevent eq "queueclientdrop" || $thisevent eq "retransdrop" 
					 || $thisevent eq "duplicatedCache" || $thisevent eq "duplicated")
							&&$tempNextState ne "drop" && $tempNextState ne "n1trans" 
							&& $tempNextState ne "n2acked" && $tempNextState ne "n2recvd"){
						$lastchoice = $tempi;
						$tempNextState = "drop";
				}
				
			}elsif($thisnode1 == $lastreceiver 
							&& $tempNextState ne "n2recvd" 
							&& $tempNextState ne "n1trans" 
							&& $tempNextState ne "n2acked"){
				if ($thisevent eq "recvd"  && $tempNextState ne "n3recvd" 
						&& $tempNextState ne "n2trans" && $tempNextState ne "n3acked"){
					$tempNextState = "n3recvd";
					$lastchoice = $tempi;
				}
				

 
				if (($thisevent eq "trans" || $thisevent eq "retrans") && $tempNextState ne "n2trans" ){
					$tempNextState = "n2trans";
					$lastchoice = $tempi;
				}
				if ($thisevent eq "acked" && $tempNextState ne "n2trans" && $tempNextState ne "n3acked"){
					$tempNextState = "n3acked";
					$lastchoice = $tempi;
				}
				
				if (($thisevent eq "msgpooldrop" || $thisevent eq "queuepooldrop" 
					 || $thisevent eq "queueclientdrop" || $thisevent eq "retransdrop"
					 || $thisevent eq "duplicatedCache" || $thisevent eq "duplicated")
					&&$tempNextState ne "drop" && $tempNextState ne "n2trans" 
					&& $tempNextState ne "n3acked" && $tempNextState ne "n3recvd"){
					$lastchoice = $tempi;
					$tempNextState = "drop";
				}
				
			}
		}
		
		#print "choice is: $lastchoice\n";
		if ($lastchoice != -1){
			push(@newnode1, $node1[$lastchoice]);
			push(@newnode2, $node2[$lastchoice]);
			push(@neweventtype, $eventtype[$lastchoice]);
			$considered[$lastchoice] = 1;
			$lastsender = $node1[$lastchoice];
			$lastreceiver = $node2[$lastchoice];
			$currentState = $tempNextState;

			if ($tempNextState eq "n2recvd" || $tempNextState eq "n3recvd"){
				$lastsender = $lastreceiver;
				$currentState = "start";
			}elsif($tempNextState eq "n2trans"){
				$currentState = "n1trans";
			}elsif( $tempNextState eq "n3acked"){
				$currentState = "n2acked";
			}
			elsif ($tempNextState eq "drop")
			{
				$needStop = 1;
			}
			#print "***$lastsender->$lastreceiver $eventtype[$lastchoice]";
		}else{
			$currentState = "notfindfirstnode";
			@firstnodeok = ();
			my $tempAvailableEvent = -1, $tempAvailableOptEvent = -1;
			for ($tempj = 0; $tempj < @node1; ++$tempj){
				if (!$considered[$tempj]){
					$firstnodeok[$tempj] = 1;
					if ($tempAvailableEvent == -1){
						$tempAvailableEvent = $tempj;
					}
					if ($tempAvailableOptEvent == -1 
					&& ($eventtype[$tempj] == "trans" ||  $eventtype[$tempj] == "retrans" ||  $eventtype[$tempj] == "acked" ||  $eventtype[$tempj] == "recvd"))
					{
						$tempAvailableOptEvent = $tempj;
					}
				}
			}
			for ($temp = 0; $temp < @node1; ++$temp){
				for ($tempj = 0; $tempj < @node1; ++$tempj){#check if $tempj will impact other nodes
					if ($tempj != $temp && !$considered[$tempj]){
						if ($node1[$tempj] == $node2[$temp]){
							$firstnodeok[$tempj] = 0;
						}
					}
				}
			}
						
			for ($tempj = 0; $tempj < @node1; ++$tempj){
				if (!$considered[$tempj] && $firstnodeok[$tempj]){
					$lastsender = $node1[$tempj];
					$lastreceiver = $lastsender;
					$currentState = "start";
					last;
				}
			}
			
			if ($currentState eq "notfindfirstnode"){
				if ($tempAvailableEvent != -1){
					$lastsender = $node1[$tempAvailableEvent];
					$lastreceiver = $lastsender;
					$currentState = "start";
				}else {
					$needStop = 1;
					#print " end \n";
				}
#				else{
#					for ($tempj = 0; $tempj < @node1; ++$tempj){
#						if (!$considered[$tempj]){
#							push(@newnode1, $node1[$tempj]);
#							push(@newnode2, $node2[$tempj]);
#							push(@neweventtype, $eventtype[$tempj]);
#						}
#					}
#					$needStop = 1;
#				}
				
			}
		
		}


	}
}

sub AnalyzeLossCause
{
	$lostCause = "unknown";
	$lostPosition = "unknown";
	if (@newnode1 > 0) {
		my $templastIndex = $#neweventtype;
		$lastEventAfterAnalysis = $neweventtype[$templastIndex];
		$lastSenderAfterAnalysis = $newnode1[$templastIndex];
		$lastReceiverAfterAnalysis = $newnode2[$templastIndex];
		if ($lastEventAfterAnalysis eq "recvd"){
			$lostPosition = $newnode2[$templastIndex];
			$lostCause = "RecvdLost";
		}elsif ($lastEventAfterAnalysis eq "acked") {
			$lostPosition = $newnode2[$templastIndex];
			$lostCause = "AckedLost";
		}elsif ($lastEventAfterAnalysis eq "trans" || $lastEventAfterAnalysis eq "retrans") {
			$lostPosition = $newnode1[$templastIndex];
			$lostCause = "TransLost";
		}else{
			$lostPosition = $newnode2[$templastIndex];
			$lostCause = $lastEventAfterAnalysis;
			#print FINAL_LOST_INFORMATION "lost cause1: xx$lastEventAfterAnalysis xx$lostCause $lostPosition\n";
 
		}
		# $lostCause = "unknown2";

		# if ($lastEventAfterAnalysis eq "msgpooldrop")
		# {
			# print FINAL_LOST_INFORMATION "lost cause2: xx$lastEventAfterAnalysis xx$lostCause $lostPosition\n";
		# }
	}
}